package cc.iotkit.manager.service.impl;

import cc.iotkit.common.api.PageRequest;
import cc.iotkit.common.api.Paging;
import cc.iotkit.data.miniAdmin.IGoodsData;
import cc.iotkit.data.system.ISysDistributorData;
import cc.iotkit.manager.dto.vo.miniAdmin.MiniAdminGoodsVo;
import cc.iotkit.manager.service.IGoodsService;
import cc.iotkit.manager.service.ISysDistributorService;
import cc.iotkit.model.miniAdmin.Goods;
import cc.iotkit.model.system.SysDistributor;
import cc.iotkit.model.system.SysOrder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Service
public class GoodsServiceImpl implements IGoodsService {

    private final IGoodsData goodsData;


    @Override
    public void save(MiniAdminGoodsVo goodsVo) {
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsVo,goods);
        goodsData.saveGoods(goods);
    }

    @Override
    public void update(MiniAdminGoodsVo goodsVo) {
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsVo,goods);
        goodsData.updateGoods(goods);
    }

    @Override
    public Paging<MiniAdminGoodsVo> getPageList(PageRequest<MiniAdminGoodsVo> query) {
        Paging pageing = new Paging<>();

        pageing = goodsData.findByConditions(query.to(Goods.class));

        List<Goods> goodList =  pageing.getRows();
        List<MiniAdminGoodsVo> goodsVos = new ArrayList<>();

        for (Goods goods:goodList){
            MiniAdminGoodsVo goodsVo = new MiniAdminGoodsVo();
            BeanUtils.copyProperties(goods,goodsVo);
            goodsVos.add(goodsVo);
        }
        pageing.setRows(goodsVos);
         return  pageing;
    }


    @Override
    public void deleteGoods(Long id) {
         goodsData.deleteGoods(id);
    }
}
